﻿namespace WMS6.Web.Billing
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;

    public class Contract : BasePage
    {
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected CheckBox chkIsActive;
        protected DropDownList ddlContractType;
        protected DropDownList ddlManageType;
        protected DropDownList ddlSettleAccount;
        protected HtmlForm F9011;
        protected HtmlInputHidden hdncalendarFlag;
        protected HtmlInputHidden hdnDataFormat;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlHead Head1;
        protected HtmlImage imgOwnerID;
        protected Label Label1;
        protected Label Label2;
        protected Label Label4;
        protected Label Label5;
        protected Label Label6;
        protected Label Label7;
        protected Label Label8;
        protected Label lblContractID;
        protected Label lblContractType;
        protected Label lblCost;
        protected Label lblCostTypes;
        protected Label lblDeposit;
        protected Label lblDescr;
        protected Label lblEndTime;
        protected Label lblGeneralLedger;
        protected Label lblManageType;
        protected Label lblMsg;
        protected Label lblOverdueRent;
        protected Label lblOwner;
        protected Label lblOwnerID;
        protected Label lblPayment;
        protected Label lblSettleAccount;
        protected Label lblStartTime;
        protected Label lblTaxGroup;
        protected Label lblWhLoad;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected ScriptManager ScriptManager1;
        protected TextBox txtAllBillsLowestCharge;
        protected TextBox txtAllBillsLowestGenralLedger;
        protected TextBox txtAllBillsLowestTagGroup;
        protected TextBox txtContractID;
        protected TextBox txtDeposit;
        protected TextBox txtDescr;
        protected TextBox txtEndTime;
        protected TextBox txtLowestInBoundBillCharge;
        protected TextBox txtLowestInBoundBillGenralLedger;
        protected TextBox txtLowestInBoundBillTagGroup;
        protected TextBox txtLowestInBoundCharge;
        protected TextBox txtLowestInBoundGenralLedger;
        protected TextBox txtLowestInBoundTagGroup;
        protected TextBox txtLowestInitialStorageBillCharge;
        protected TextBox txtLowestInitialStorageBillGenralLedger;
        protected TextBox txtLowestInitialStorageBillTagGroup;
        protected TextBox txtLowestInitialStorageCharge;
        protected TextBox txtLowestInitialStorageGenralLedger;
        protected TextBox txtLowestInitialStorageTagGroup;
        protected TextBox txtLowestOutBoundCharge;
        protected TextBox txtLowestOutBoundGenralLedger;
        protected TextBox txtLowestOutBoundTagGroup;
        protected TextBox txtLowestRecurringStorageBillCharge;
        protected TextBox txtLowestRecurringStorageBillGenralLedger;
        protected TextBox txtLowestRecurringStorageBillTagGroup;
        protected TextBox txtOverdueRent;
        protected TextBox txtOwnerID;
        protected TextBox txtPayment;
        protected TextBox txtStartTime;
        protected TextBox txtWhLoad;
        protected UpdatePanel up1;

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            this.txtContractID.Text = "";
            this.txtContractID.Enabled = true;
            base.DataEntityViewState = null;
            this.hdnFlag.Value = "";
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtContractID.Text.Trim()))
            {
                try
                {
                    List<string> ContractIDS = new List<string> {
                        this.txtContractID.Text.Trim()
                    };
                    this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Delete(ContractIDS, "CONTRACT") + base.GetGlobalMsgResource("F9110_009");
                    this.btnCopy.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnSave.Enabled = false;
                    base.DataEntityViewState = null;
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F9110_010") + ex.Message;
                }
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("ContractQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("ContractList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            base.Server.Transfer(string.Format("Contract.aspx?from={0}", base.UrlEncode(this.hdnFrom.Value.Trim())), false);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtOwnerID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9110_002");
            }
            else if (string.IsNullOrEmpty(this.txtStartTime.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9110_003");
                this.txtStartTime.Focus();
            }
            else if (string.IsNullOrEmpty(this.txtEndTime.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9110_004");
                this.txtEndTime.Focus();
            }
            else if (this.hdnFlag.Value == "1")
            {
                this.UpdateContractInfo();
            }
            else
            {
                this.InsertContractInfo();
            }
        }

        private void InsertContractInfo()
        {
            try
            {
                ContractInfo contractInfo = new ContractInfo {
                    ContractID = this.txtContractID.Text,
                    OwnerID = this.txtOwnerID.Text,
                    Descr = this.txtDescr.Text,
                    ContractType = this.ddlContractType.SelectedValue,
                    SettleAccount = this.ddlSettleAccount.SelectedValue
                };
                if (!string.IsNullOrEmpty(this.txtPayment.Text))
                {
                    contractInfo.Payment = decimal.Parse(this.txtPayment.Text);
                }
                else
                {
                    contractInfo.Payment = 0M;
                }
                contractInfo.StartTime = Convert.ToDateTime(this.txtStartTime.Text);
                contractInfo.EndTime = Convert.ToDateTime(this.txtEndTime.Text);
                contractInfo.WhLoad = this.txtWhLoad.Text;
                contractInfo.ManageType = this.ddlManageType.SelectedValue;
                if (!string.IsNullOrEmpty(this.txtDeposit.Text))
                {
                    contractInfo.Deposit = decimal.Parse(this.txtDeposit.Text);
                }
                else
                {
                    contractInfo.Deposit = 0M;
                }
                if (!string.IsNullOrEmpty(this.txtOverdueRent.Text))
                {
                    contractInfo.OverdueRent = decimal.Parse(this.txtOverdueRent.Text);
                }
                else
                {
                    contractInfo.OverdueRent = 0M;
                }
                contractInfo.IsActive = this.chkIsActive.Checked ? "Y" : "N";
                contractInfo.CreatedBy = base.UserInfo.UserID;
                if (!string.IsNullOrEmpty(this.txtLowestInBoundCharge.Text))
                {
                    contractInfo.LowestInboundCharge = decimal.Parse(this.txtLowestInBoundCharge.Text);
                }
                else
                {
                    contractInfo.LowestInboundCharge = 0M;
                }
                contractInfo.LowestInboundTaxGroup = this.txtLowestInBoundTagGroup.Text;
                contractInfo.LowestInboundGeneralLedger = this.txtLowestInBoundGenralLedger.Text;
                if (!string.IsNullOrEmpty(this.txtLowestOutBoundCharge.Text))
                {
                    contractInfo.LowestOutboundCharge = decimal.Parse(this.txtLowestOutBoundCharge.Text);
                }
                else
                {
                    contractInfo.LowestOutboundCharge = 0M;
                }
                contractInfo.LowestOutboundTaxGroup = this.txtLowestOutBoundTagGroup.Text;
                contractInfo.LowestOutboundGeneralLedger = this.txtLowestOutBoundGenralLedger.Text;
                if (!string.IsNullOrEmpty(this.txtLowestInitialStorageCharge.Text))
                {
                    contractInfo.LowestInitialStorageCharge = decimal.Parse(this.txtLowestInitialStorageCharge.Text);
                }
                else
                {
                    contractInfo.LowestInitialStorageCharge = 0M;
                }
                contractInfo.LowestInitialStorageTaxGroup = this.txtLowestInitialStorageTagGroup.Text;
                contractInfo.LowestInitialStorageGeneralLedger = this.txtLowestInitialStorageGenralLedger.Text;
                if (!string.IsNullOrEmpty(this.txtLowestInitialStorageCharge.Text))
                {
                    contractInfo.LowestInboundBillCharge = decimal.Parse(this.txtLowestInBoundBillCharge.Text);
                }
                else
                {
                    contractInfo.LowestInboundBillCharge = 0M;
                }
                contractInfo.LowestInboundBillTaxGroup = this.txtLowestInBoundBillTagGroup.Text;
                contractInfo.LowestInboundBillGeneralLedger = this.txtLowestInBoundBillGenralLedger.Text;
                if (!string.IsNullOrEmpty(this.txtLowestInitialStorageBillCharge.Text))
                {
                    contractInfo.LowestInitialStorageBillCharge = decimal.Parse(this.txtLowestInitialStorageBillCharge.Text);
                }
                else
                {
                    contractInfo.LowestInitialStorageBillCharge = 0M;
                }
                contractInfo.LowestInitialStorageBillTaxGroup = this.txtLowestInitialStorageBillTagGroup.Text;
                contractInfo.LowestInitialStorageBillGeneralLedger = this.txtLowestInitialStorageBillGenralLedger.Text;
                if (!string.IsNullOrEmpty(this.txtLowestRecurringStorageBillCharge.Text))
                {
                    contractInfo.LowestRecurringStorageBillCharge = decimal.Parse(this.txtLowestRecurringStorageBillCharge.Text);
                }
                else
                {
                    contractInfo.LowestRecurringStorageBillCharge = 0M;
                }
                contractInfo.LowestRecurringStorageBillTaxGroup = this.txtLowestRecurringStorageBillTagGroup.Text;
                contractInfo.LowestRecurringStorageBillGeneralLedger = this.txtLowestRecurringStorageBillGenralLedger.Text;
                if (!string.IsNullOrEmpty(this.txtAllBillsLowestCharge.Text))
                {
                    contractInfo.AllBillsLowestCharge = decimal.Parse(this.txtAllBillsLowestCharge.Text);
                }
                else
                {
                    contractInfo.AllBillsLowestCharge = 0M;
                }
                contractInfo.AllBillsLowestGeneralLedger = this.txtAllBillsLowestGenralLedger.Text;
                contractInfo.AllBillsLowestTaxGroup = this.txtAllBillsLowestTagGroup.Text;
                string strContractID = string.Empty;
                strContractID = BaseInfoServiceClientProxy.GetInstance().Insert(contractInfo, "CONTRACT");
                this.hdnFlag.Value = "1";
                base.DataEntityViewState = contractInfo;
                this.txtContractID.Text = strContractID;
                this.txtContractID.Enabled = false;
                this.lblMsg.Text = strContractID + base.GetGlobalMsgResource("F9110_007");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9110_008") + ex.Message;
            }
        }

        private void LoadData()
        {
            base.BindData2DDLBySysCode(this.ddlContractType, "ContractType");
            base.BindData2DDLBySysCode(this.ddlSettleAccount, "SettlementMode");
            base.BindData2DDLBySysCode(this.ddlManageType, "ManageType");
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnFlag.Value = "1";
                this.SetContractInfo2Form();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetContractInfo2Form()
        {
            try
            {
                string strCustomerID = base.Request.QueryString["ID"].ToString();
                ContractInfo contractInfo = (ContractInfo) BaseInfoServiceClientProxy.GetInstance().GetInfoByID(strCustomerID, "CONTRACT");
                if (contractInfo != null)
                {
                    base.DataEntityViewState = contractInfo;
                    this.txtContractID.Text = contractInfo.ContractID;
                    this.txtOwnerID.Text = contractInfo.OwnerID;
                    this.txtDescr.Text = contractInfo.Descr;
                    this.ddlContractType.SelectedValue = contractInfo.ContractType;
                    this.ddlSettleAccount.SelectedValue = contractInfo.SettleAccount;
                    this.txtPayment.Text = base.GetPreferenceDigit(contractInfo.Payment);
                    this.txtStartTime.Text = base.GetPreferenceShortDate(contractInfo.StartTime);
                    this.txtEndTime.Text = base.GetPreferenceShortDate(contractInfo.EndTime);
                    this.txtWhLoad.Text = contractInfo.WhLoad;
                    this.ddlManageType.SelectedValue = contractInfo.ManageType;
                    this.txtOverdueRent.Text = base.GetPreferenceDigit(contractInfo.OverdueRent);
                    this.txtDeposit.Text = base.GetPreferenceDigit(contractInfo.Deposit);
                    this.chkIsActive.Checked = contractInfo.IsActive == "Y";
                    this.txtLowestInBoundCharge.Text = base.GetPreferenceDigit(contractInfo.LowestInboundCharge);
                    this.txtLowestInBoundTagGroup.Text = contractInfo.LowestInboundTaxGroup;
                    this.txtLowestInBoundGenralLedger.Text = contractInfo.LowestInboundGeneralLedger;
                    this.txtLowestOutBoundCharge.Text = base.GetPreferenceDigit(contractInfo.LowestOutboundCharge);
                    this.txtLowestOutBoundTagGroup.Text = contractInfo.LowestOutboundTaxGroup;
                    this.txtLowestOutBoundGenralLedger.Text = contractInfo.LowestOutboundGeneralLedger;
                    this.txtLowestInitialStorageCharge.Text = base.GetPreferenceDigit(contractInfo.LowestInitialStorageCharge);
                    this.txtLowestInitialStorageTagGroup.Text = contractInfo.LowestInitialStorageTaxGroup;
                    this.txtLowestInitialStorageGenralLedger.Text = contractInfo.LowestInitialStorageGeneralLedger;
                    this.txtLowestInBoundBillCharge.Text = base.GetPreferenceDigit(contractInfo.LowestInboundBillCharge);
                    this.txtLowestInBoundBillTagGroup.Text = contractInfo.LowestInboundBillTaxGroup;
                    this.txtLowestInBoundBillGenralLedger.Text = contractInfo.LowestInboundBillGeneralLedger;
                    this.txtLowestInitialStorageBillCharge.Text = base.GetPreferenceDigit(contractInfo.LowestInitialStorageBillCharge);
                    this.txtLowestInitialStorageBillTagGroup.Text = contractInfo.LowestInitialStorageBillTaxGroup;
                    this.txtLowestInitialStorageBillGenralLedger.Text = contractInfo.LowestInitialStorageBillGeneralLedger;
                    this.txtLowestRecurringStorageBillCharge.Text = base.GetPreferenceDigit(contractInfo.LowestRecurringStorageBillCharge);
                    this.txtLowestRecurringStorageBillTagGroup.Text = contractInfo.LowestRecurringStorageBillTaxGroup;
                    this.txtLowestRecurringStorageBillGenralLedger.Text = contractInfo.LowestRecurringStorageBillGeneralLedger;
                    this.txtAllBillsLowestCharge.Text = base.GetPreferenceDigit(contractInfo.AllBillsLowestCharge);
                    this.txtAllBillsLowestGenralLedger.Text = contractInfo.AllBillsLowestGeneralLedger;
                    this.txtAllBillsLowestTagGroup.Text = contractInfo.AllBillsLowestTaxGroup;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdateContractInfo()
        {
            try
            {
                ContractInfo contractInfo = base.DataEntityViewState as ContractInfo;
                contractInfo.OwnerID = this.txtOwnerID.Text;
                contractInfo.Descr = this.txtDescr.Text;
                contractInfo.ContractType = this.ddlContractType.SelectedValue;
                contractInfo.SettleAccount = this.ddlSettleAccount.SelectedValue;
                if (!string.IsNullOrEmpty(this.txtPayment.Text))
                {
                    contractInfo.Payment = decimal.Parse(this.txtPayment.Text);
                }
                else
                {
                    contractInfo.Payment = 0M;
                }
                contractInfo.StartTime = Convert.ToDateTime(this.txtStartTime.Text);
                contractInfo.EndTime = Convert.ToDateTime(this.txtEndTime.Text);
                contractInfo.WhLoad = this.txtWhLoad.Text;
                contractInfo.ManageType = this.ddlManageType.SelectedValue;
                if (!string.IsNullOrEmpty(this.txtOverdueRent.Text))
                {
                    contractInfo.OverdueRent = decimal.Parse(this.txtOverdueRent.Text);
                }
                else
                {
                    contractInfo.OverdueRent = 0M;
                }
                if (!string.IsNullOrEmpty(this.txtDeposit.Text))
                {
                    contractInfo.Deposit = decimal.Parse(this.txtDeposit.Text);
                }
                else
                {
                    contractInfo.Deposit = 0M;
                }
                contractInfo.IsActive = this.chkIsActive.Checked ? "Y" : "N";
                contractInfo.UpdatedBy = base.UserInfo.UserID;
                if (!string.IsNullOrEmpty(this.txtLowestInBoundCharge.Text))
                {
                    contractInfo.LowestInboundCharge = decimal.Parse(this.txtLowestInBoundCharge.Text);
                }
                else
                {
                    contractInfo.LowestInboundCharge = 0M;
                }
                contractInfo.LowestInboundTaxGroup = this.txtLowestInBoundTagGroup.Text;
                contractInfo.LowestInboundGeneralLedger = this.txtLowestInBoundGenralLedger.Text;
                if (!string.IsNullOrEmpty(this.txtLowestOutBoundCharge.Text))
                {
                    contractInfo.LowestOutboundCharge = decimal.Parse(this.txtLowestOutBoundCharge.Text);
                }
                else
                {
                    contractInfo.LowestOutboundCharge = 0M;
                }
                contractInfo.LowestOutboundTaxGroup = this.txtLowestOutBoundTagGroup.Text;
                contractInfo.LowestOutboundGeneralLedger = this.txtLowestOutBoundGenralLedger.Text;
                if (!string.IsNullOrEmpty(this.txtLowestInitialStorageCharge.Text))
                {
                    contractInfo.LowestInitialStorageCharge = decimal.Parse(this.txtLowestInitialStorageCharge.Text);
                }
                else
                {
                    contractInfo.LowestInitialStorageCharge = 0M;
                }
                contractInfo.LowestInitialStorageTaxGroup = this.txtLowestInitialStorageTagGroup.Text;
                contractInfo.LowestInitialStorageGeneralLedger = this.txtLowestInitialStorageGenralLedger.Text;
                if (!string.IsNullOrEmpty(this.txtLowestInitialStorageCharge.Text))
                {
                    contractInfo.LowestInboundBillCharge = decimal.Parse(this.txtLowestInBoundBillCharge.Text);
                }
                else
                {
                    contractInfo.LowestInboundBillCharge = 0M;
                }
                contractInfo.LowestInboundBillTaxGroup = this.txtLowestInBoundBillTagGroup.Text;
                contractInfo.LowestInboundBillGeneralLedger = this.txtLowestInBoundBillGenralLedger.Text;
                if (!string.IsNullOrEmpty(this.txtLowestInitialStorageBillCharge.Text))
                {
                    contractInfo.LowestInitialStorageBillCharge = decimal.Parse(this.txtLowestInitialStorageBillCharge.Text);
                }
                else
                {
                    contractInfo.LowestInitialStorageBillCharge = 0M;
                }
                contractInfo.LowestInitialStorageBillTaxGroup = this.txtLowestInitialStorageBillTagGroup.Text;
                contractInfo.LowestInitialStorageBillGeneralLedger = this.txtLowestInitialStorageBillGenralLedger.Text;
                if (!string.IsNullOrEmpty(this.txtLowestRecurringStorageBillCharge.Text))
                {
                    contractInfo.LowestRecurringStorageBillCharge = decimal.Parse(this.txtLowestRecurringStorageBillCharge.Text);
                }
                else
                {
                    contractInfo.LowestRecurringStorageBillCharge = 0M;
                }
                contractInfo.LowestRecurringStorageBillTaxGroup = this.txtLowestRecurringStorageBillTagGroup.Text;
                contractInfo.LowestRecurringStorageBillGeneralLedger = this.txtLowestRecurringStorageBillGenralLedger.Text;
                if (!string.IsNullOrEmpty(this.txtAllBillsLowestCharge.Text))
                {
                    contractInfo.AllBillsLowestCharge = decimal.Parse(this.txtAllBillsLowestCharge.Text);
                }
                else
                {
                    contractInfo.AllBillsLowestCharge = 0M;
                }
                contractInfo.AllBillsLowestGeneralLedger = this.txtAllBillsLowestGenralLedger.Text;
                contractInfo.AllBillsLowestTaxGroup = this.txtAllBillsLowestTagGroup.Text;
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Update(contractInfo, "CONTRACT") + base.GetGlobalMsgResource("F9110_005");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9110_006") + ex.Message;
            }
        }
    }
}

